package com.wsoft.cms.controller;

import com.wsoft.cms.query.category.CategoryInsertQuery;
import com.wsoft.cms.query.category.CategoryUpdateQuery;
import com.wsoft.cms.service.ICmsCategoryService;
import com.wsoft.cms.vo.category.CmsCategoryVO;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 公告类型表 前端控制器
 * @author Z001
 * @since 2024年11月27日 10:39:16
 */
@RestController
@Tag(name = "分类")
@RequestMapping("/cms/cate")
public class CmsCategoryController {

    @Resource
    private ICmsCategoryService categoryService;

    @Operation(summary = "新增分类", description = "新增分类")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid CategoryInsertQuery query) {
        boolean b = categoryService.insertCategory(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "编辑分类", description = "编辑分类")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid CategoryUpdateQuery query) {
        boolean b = categoryService.updateCategory(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "分类tree", description = "分类tree")
    @PostMapping("/tree")
    public ResponseResult tree() {
        List<CmsCategoryVO> list = categoryService.tree(false,null);
        return ResponseResult.success(list);
    }


    @Operation(summary = "删除分类", description = "删除分类")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = categoryService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }


    @Operation(summary = "分类详情", description = "分类详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(categoryService.get(id));
    }
}
